package com.finger.forest.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


import com.finger.forest.R;

import java.util.List;

public class PatrolStaticExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private List<String> groupName;
    private List<List<String>> childName;

    public PatrolStaticExpandableListAdapter(Context mContext, List<String> groupName, List<List<String>> childName) {
        this.mContext = mContext;
        this.groupName = groupName;
        this.childName = childName;
    }

    @Override
    public int getGroupCount() {
        return groupName.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public String getGroup(int groupPosition) {
        return groupName.get(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        convertView = View.inflate(mContext, R.layout.item_group_name, null);

        TextView groupName = (TextView) convertView.findViewById(R.id.group_name);
        groupName.setText(getGroup(groupPosition));

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childName.get(groupPosition).size();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        return childName.get(groupPosition).get(childPosition);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        convertView = View.inflate(mContext, R.layout.item_child_name, null);

        TextView childName = (TextView) convertView.findViewById(R.id.child_name);
        childName.setText(getChild(groupPosition, childPosition));
        childName.setTag("");

        childName.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                int count = parent.getChildCount();
                for (int i=0; i<count; i++){ //全部设置为未选择
                    if (parent.getChildAt(i) instanceof TextView){
                        ((TextView)parent.getChildAt(i)).setCompoundDrawablesWithIntrinsicBounds(
                                mContext.getResources().getDrawable(R.mipmap.radio_unselect,null),null,null,null);
                        ((TextView)parent.getChildAt(i)).setTag("");
                    }
                }

                //当前TextView设置为已选择
                childName.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getResources().getDrawable(R.mipmap.radio_select,null),null,null,null);
                childName.setTag("true");

            }
        });

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}